import { useTranslation } from "react-i18next";
import "./index.less";
import { useEffect, useState } from "react";
import { BasicProTable, useProTable } from "@/components/ViewComponents/proTable";
import { drugInfoApi } from "@/api/erp/drug/drugCatalogue";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { useLocation } from "react-router-dom";
import { treatmentPhm } from "@/api/ImmigrationDictionary";
import { message } from "antd";

const index = () => {
	const { t } = useTranslation();
	const en_vi = "MedicalExamination.prescribe";
	const [patientId, setPatientId] = useState("");
	const [patCardno, setPatCardno] = useState("");
	const location = useLocation();
	const defaultColumns = [
		{
			title: t(`${en_vi}.drugCode`),
			dataIndex: "drugId"
		},
		{
			title: t(`${en_vi}.drugName`),
			dataIndex: "drugName"
		},
		{
			title: t(`${en_vi}.drugBarcode`),
			dataIndex: "barCode"
		},
		{
			title: t(`${en_vi}.drugCategoryCode`),
			dataIndex: "drugCats"
		},
		{
			title: t(`${en_vi}.activeIngredients`),
			dataIndex: "activeIngre"
		},
		{
			title: t(`${en_vi}.sideEffects`),
			dataIndex: "sideEffects"
		},
		{
			title: t(`${en_vi}.specifications`),
			dataIndex: "drugSpec"
		},
		{
			title: t(`${en_vi}.storageConditions`),
			dataIndex: "storageCond"
		},
		{
			title: t(`${en_vi}.remarks`),
			dataIndex: "remark"
		},
		{
			title: t(`${en_vi}.drugInteractions`),
			dataIndex: "drugAds"
		},
		{
			title: t(`${en_vi}.weightKg`),
			dataIndex: "weight"
		},
		{
			title: t(`${en_vi}.purchasePrice`),
			fixed: "right",
			width: 50,
			dataIndex: "purPrice"
		},
		{
			title: t(`${en_vi}.sellingPrice`),
			fixed: "right",
			width: 50,
			dataIndex: "salePrice"
		},
		{
			title: t(`${en_vi}.minimumPrice`),
			fixed: "right",
			width: 50,
			dataIndex: "minPrice"
		},
		{
			title: t(`drugStorage.equipment.device.quantity`),
			fixed: "right",
			width: 50,
			dataIndex: "number",
			editable: true,
			type: "InputNumber",
			componentProps: {
				value: 1
			}
		}
	];
	const [registerTable, { submit }] = useProTable({
		api: drugInfoApi.drugsList,
		key: "drugId",
		type: "Checkbox",
		defaultColumns,
		default: {
			number: 1
		}
	});
	const drugOrders = async () => {
		const treatmentphms = await submit();
		treatmentphms.forEach((item: any) => {
			item.drugCount = item.number;
		});
		const res = await treatmentPhm({
			patientId,
			patCardno,
			treatmentphms
		});
		if (!res) message.error(t("kevin_ui.addFailed"));
		message.success(t("kevin_ui.addedSuccessfully"));
	};
	const handlerToolbarAction = async (key: any) => {
		switch (key) {
<<<<<<< HEAD
			case "back":
				console.log();
				if (window.location.href.includes("EmergencyDictionaryR1")) {
					navigate("/EmergencyDictionaryR1/4.14");
				} else {
					navigate("/MedicalExamination/4.14");
				}
				break;
=======
>>>>>>> b864b84b91f980477faac743f54f32d3c3f3d4a9
			case "save":
				drugOrders();
				return;
		}
	};
	useEffect(() => {
		if (location.state) {
			setPatientId(location.state.patientId);
			setPatCardno(location.state.patCardno);
		}
	}, []);
	return (
		<div className=" page">
			<div className="page-content">
				<BasicProTable register={registerTable}></BasicProTable>
			</div>
			<Toolbar btnList={["save"]} action={handlerToolbarAction} />
		</div>
	);
};
export default index;
